#! _*_ coding:utf8 _*_
import school
from school.utils import create_school
from student.serializers import SClassSerializer
from teacher.models import Teacher


def schools_of_teacher(teacher):
    '''
    返回该老师的学校列表，如果没有学校，将以该都的名称自动创建一个学校
    :param teacher: 老师实例
    :return: 学校列表
    '''

    schools = teacher.teacher_schools.all()
    if not schools:
        school = create_school(teacher.me.first_name)
        teacher.teacher_schools.add(school)
        teacher.save()
        schools = teacher.teacher_schools.all()

    return schools


def create_class(klass):
    '''
    创建课程，包括其中的学生
    :param klass: 课程
    :return: 课程
    '''

    clz = SClassSerializer(data=klass)
    clz.is_valid()
    c = clz.save()
    c.save()
    return c


def create_teacher(user):
    t = Teacher.objects.create(me=user).save()
    return t